package com.example.jdbc.druid;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class DruridConfiguration {
    private static final Logger logger=LoggerFactory.getLogger(DruridConfiguration.class);

    private static final String DB_PREFIX="spring.datasource";

    @Bean
    public ServletRegistrationBean druidServlet(){
        logger.info("init Druid Servlet Configuartion");
        ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(new StatViewServlet(),
                "/druid/*");
        //ip 白名单
        servletRegistrationBean.addInitParameter("allow","192.168.2.25,127.0.0.1");
        //ip 黑名单（共同存在时，deny优先于allow）
        servletRegistrationBean.addInitParameter("deny","192.168.1.100");
        //控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername","admin");
        servletRegistrationBean.addInitParameter("loginPassword","8527");
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return  filterRegistrationBean;
    }

}
